package com.rytong.tools.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

public class LPScrollView extends LPLayout {
    // 纵向滑动域y方向可滑动最大距离,该距离由其子元素高度和其自身的可视高度差决定
    private int limitY_;
    // 是否是局部滚动的控件。
    public boolean isPartlyScroll_ = false;
    // 需要处理的tr。
    public Component currentTR_ = null;
    // 需要处理的按钮。
    public Component currentButton_ = null;
    // 需要进行焦点释放的组件。
    public Object focusObj_ = null;
    
    public class MyLPScrollView extends ScrollView implements CompositedComponent {

        public MyLPScrollView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Component composited() {
            // TODO Auto-generated method stub
            return LPScrollView.this;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            ViewGroup vg = (ViewGroup) getParent();
            // vg.requestDisallowInterceptTouchEvent(true);
            if(vg == null){
            	return super.onTouchEvent(event);
            } 
            int sY = getScrollY();
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                yDown_ = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
            	if(focusObj_ != null){
            		if(focusObj_ instanceof Component){
            			return ((Component) focusObj_).realView_.onTouchEvent(event);
            		} else if(focusObj_ instanceof LPSelect){
            			// 移动的时候恢复select的背景图。
            			if(((LPSelect) focusObj_).oldBG_ == null || "".equals(((LPSelect) focusObj_).oldBG_)){
            				realView_.setBackgroundColor(0);
            			} else {
            				realView_.setBackgroundColor(Color.parseColor(Component.fixedPX(
            						((LPSelect) focusObj_).oldBG_)));
            			}
            		}
            	}
                int offset = (int) Math.abs(yDown_ - event.getY());
                if (offset > offset_) {
                	// 移动tr恢复颜色。
                	if(currentTR_ != null){
                		currentTR_.setCssStyle(((LPLayout) currentTR_).oldTRCssStyle_);
                		((MyLPLayout)currentTR_.realView_).isSpecialTRBg_ = ((LPLayout) currentTR_).oldSpecialTR_;
                		currentTR_.realView_.invalidate();
                		currentTR_ = null;
                	}
                	// 移动按钮的时候恢复按钮的颜色。
                	if(currentButton_ != null && currentButton_.realView_.isEnabled() == true
                			&& currentButton_.upBG_ != -1){
                		currentButton_.realView_.setBackgroundResource(currentButton_.upBG_);
                		currentButton_ = null;
                	}
            	}
                if(isPartlyScroll_ == false){
                	float yMove = event.getY();
                	if ((yMove - yDown_) > 0) {
                		if (sY <= 0)
                			vg.requestDisallowInterceptTouchEvent(false);
                		
                	} else if ((yMove - yDown_) < 0) {
                		if (sY >= getLimitY()) {
                			vg.requestDisallowInterceptTouchEvent(false);
                		}
                	}
                	yDown_ = yMove;
                }
                break;
            case MotionEvent.ACTION_UP:
            	if(focusObj_ != null){
            		if(focusObj_ instanceof Component){
            			return ((Component) focusObj_).realView_.onTouchEvent(event);
            		} else if(focusObj_ instanceof LPSelect){
            			// 抬起的时候恢复select的背景图。
            			if(((LPSelect) focusObj_).oldBG_ == null || "".equals(((LPSelect) focusObj_).oldBG_)){
            				realView_.setBackgroundColor(0);
            			} else {
            				realView_.setBackgroundColor(Color.parseColor(Component.fixedPX(
            						((LPSelect) focusObj_).oldBG_)));
            			}
            		}
            	}
            	// 抬起tr恢复颜色。
            	if(currentTR_ != null){
            		currentTR_.setCssStyle(((LPLayout) currentTR_).oldTRCssStyle_);
        			((MyLPLayout)currentTR_.realView_).isSpecialTRBg_ = ((LPLayout) currentTR_).oldSpecialTR_;
        			currentTR_.realView_.invalidate();
        			currentTR_ = null;
            	}
            	// 抬起按钮的时候恢复按钮的颜色。
            	if(currentButton_ != null && currentButton_.realView_.isEnabled() == true
            			&& currentButton_.upBG_ != -1){
            		currentButton_.realView_.setBackgroundResource(currentButton_.upBG_);
            		currentButton_ = null;
            	}
            	break;
            default:
                break;
            }
            return super.onTouchEvent(event);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            ViewGroup vg = (ViewGroup) getParent();
            if (vg == null) {
                return super.onInterceptTouchEvent(ev);
            } else {
                if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                    // 将父scrollview的滚动事件拦截
                    vg.requestDisallowInterceptTouchEvent(true);
                } else if (ev.getAction() == MotionEvent.ACTION_UP) {
                    // 把滚动事件恢复给父Scrollview
                    vg.requestDisallowInterceptTouchEvent(false);
                } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                }
            }
            return super.onInterceptTouchEvent(ev);
        }
    }

    public LPScrollView(Context context) {
        // TODO Auto-generated constructor stub
        realView_ = new MyLPScrollView(context);
    }

    public void addView(Component c) {
        if (null != realView_ && null != c && null != c.realView_)
            ((MyLPScrollView) realView_).addView(c.realView_);
    }

    @Override
    public void mouldW() throws Exception {
    }

    @Override
    public void adjustXY() {
    }

    public int getChildCount() {
        // TODO Auto-generated method stub
        MyLPScrollView temp = (MyLPScrollView) realView_;
        return temp.getChildCount();
    }

    protected void setLimitY(int y) {
        limitY_ = y;
    }

    protected int getLimitY() {
        return limitY_;
    }

    public int indexOfChild(View realView) {
        // TODO Auto-generated method stub
        MyLPScrollView temp = (MyLPScrollView) realView_;
        return temp.indexOfChild(realView);
    }
    
    // 拿到component。
    public Component getComponent(Object v) {
        if (v instanceof CompositedComponent) {
            CompositedComponent cc = (CompositedComponent) v;
            return cc.composited();
        }
        return null;
    }
}
